package com.htrq.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htrq.dao.CNGCHKDao;
import com.htrq.entity.CNGDatachk;
import com.htrq.entity.Page;
import com.htrq.entity.User;

@Service
@Transactional(value = "txManager")
public class CNGCHKService {
	@Resource
	private CNGCHKDao chkDao;

	public List<CNGDatachk> getAllchk(CNGDatachk chk, Page page) {
		// TODO Auto-generated method stub
		List<CNGDatachk> list = chkDao.getAllchk(chk, page);
		return list;
	}
	public Long getTotal(CNGDatachk chk) {
		// TODO Auto-generated method stub
		return chkDao.getTotal(chk);
	}
	public boolean saveChk(HttpServletRequest request, CNGDatachk chk) {
		// TODO Auto-generated method stub
		try {
			User user = (User) request.getSession().getAttribute("user");
			chk.setCreater(user.getRelname());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String root = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " ";
			String str1 = root + chk.getStartHour() + ":" + chk.getStartMinute() + ":00";
			String str2 = root + chk.getEndHour() + ":" + chk.getEndMinute() + ":00";
			if (chk.getXstartHour() != "" && chk.getXstartMinute() != "") {
				String str3 = root + chk.getXstartHour() + ":" + chk.getXstartMinute() + ":00";
				Date date3 = sdf.parse(str3);
				chk.setXstartDate(date3);
			}
			if (chk.getXendHour() != "" && chk.getEndMinute() != "") {
				String str4 = root + chk.getXendHour() + ":" + chk.getXendMinute() + ":00";
				Date date4 = sdf.parse(str4);
				chk.setEndDate(date4);
			}
			Date date1 = sdf.parse(str1);
			Date date2 = sdf.parse(str2);
			chk.setStartDate(date1);
			chk.setEndDate(date2);
			chkDao.saveChk(chk);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public CNGDatachk getChkById(String parameter) throws ParseException {
		// TODO Auto-generated method stub
		CNGDatachk chk = chkDao.get(parameter);
	/*	chk.setChkDate(new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(chk
				.getChkDate())));*/
		return chk;
	}
	public boolean updateChk(CNGDatachk chk) {
		// TODO Auto-generated method stub
		try {
			chkDao.saveOrUpdate(chk);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public boolean del(CNGDatachk chk) {
		// TODO Auto-generated method stub
		try {
			chkDao.delete(chk);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public List<Object[]> exportbyForm(CNGDatachk chk) {
		//TODO Auto-generated method stub
		List<Object[]> list = chkDao.exportbyForm(chk);
		return list;
	}
}
